package com.iotimc.attendancesystem;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.iotimc.attendancesystem.attendance.service.AttendanceService;
import com.iotimc.attendancesystem.dao.GreenDaoManager;
import com.iotimc.attendancesystem.entity.BasicSettingEntity;
import com.iotimc.attendancesystem.io.ButtonCommonPresenter;
import com.iotimc.attendancesystem.io.CardCommonPresenter;
import com.iotimc.attendancesystem.io.IoCommonPresenter;
import com.iotimc.attendancesystem.util.CrashHandler;
import com.iotimc.attendancesystem.util.TtsUtil;
import com.iotimc.comass.util.IotimcSerialHelper;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * @author david
 * @date 2017/11/27
 */

public class App extends Application {
    private static final String TAG = "App";
    private static App instance;
    private BasicSettingEntity basicSettingEntity;
    private AttendanceService.Client mClient;
    public static AttendanceService attendanceService;
    public String getCookie() {
        return cookie;
    }

    public void setCookie(String cookie) {
        this.cookie = cookie;
    }

    private String cookie;

    public static App getInstance() {
        return instance;
    }

    public static Context getAppContext() {
        return instance.getApplicationContext();
    }

    public BasicSettingEntity getBasicSettingEntity() {
        return basicSettingEntity;
    }

    public void setBasicSettingEntity(BasicSettingEntity basicSettingEntity) {
        this.basicSettingEntity = basicSettingEntity;
    }

    /**
     * @return the main resources from the Application
     */
    public static Resources getAppResources() {
        return instance.getResources();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        TtsUtil.init(this);
        CrashHandler.getInstance().init(this);
        IotimcSerialHelper.getCmdInitFile(this);
        basicSettingEntity =  GreenDaoManager.getInstance().getSession().getBasicSettingEntityDao().load(1L);
        if (basicSettingEntity == null){
            basicSettingEntity =new BasicSettingEntity();
            basicSettingEntity.setId(1L);
            GreenDaoManager.getInstance().getSession().getBasicSettingEntityDao().insert(basicSettingEntity);
        }
        Observable.timer(5, TimeUnit.SECONDS,Schedulers.io())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        IoCommonPresenter.getInstance();
                        CardCommonPresenter.getInstance();
                        ButtonCommonPresenter.getInstance();
                        GreenDaoManager.getInstance();

                        Log.i(TAG, "accept: "+basicSettingEntity.getDeviceType().toString());
                    }
                });
    }

    private void initAttendanceService(){
        mClient = new AttendanceService.Client(getAppContext(), new AttendanceService.Client.Callback() {
            @Override
            public void onConnected(AttendanceService service) {
                attendanceService = service;
            }

            @Override
            public void onDisconnected() {
                attendanceService = null;
            }
        });
    }


}
